<?php
//**---------------------------------------------------------------------+
//** 服务层文件 -- 文章标题
//**---------------------------------------------------------------------+
//** 版权所有：江西佰商科技有限公司. 官网：https://www.tigshop.com
//**---------------------------------------------------------------------+
//** 作者：Tigshop团队，yq@tigshop.com
//**---------------------------------------------------------------------+
//** 提示：Tigshop商城系统为非免费商用系统，未经授权，严禁使用、修改、发布
//**---------------------------------------------------------------------+

namespace app\service\api\admin\common;

use app\model\common\Locales;
use app\model\common\Translations;
use app\model\content\Article;
use app\model\content\ArticleCategory;
use app\model\product\ProductArticle;
use app\service\core\BaseService;
use exceptions\ApiException;

/**
 * 文章标题服务类
 */
class TranslationsService extends BaseService
{
    protected Translations $translations;

    public function __construct(Translations $translations)
    {
        $this->translations = $translations;
    }


    /**
     * 筛选查询
     *
     * @param array $filter
     * @return object
     */
    protected function filterQuery(array $filter): object
    {
        $query = $this->translations->query();
        // 处理筛选条件

        if (!empty($filter['locale_code'])) {
            $locale = Locales::where('locale_code', $filter['locale_code'])->where('is_enabled', 1)->find();
            if ($locale) {
                $query->where('locale_id', $locale->id);
            } else {
                $query->where('locale_id', -1);
            }
        }
        if (isset($filter['locale_id']) && $filter['locale_id'] > 0) {
            $query->where('locale_id', $filter['locale_id']);
        }

        if (!empty($filter['translation_name'])) {
            $query->where('translation_name', $filter['translation_name']);
        }

        if (!empty($filter['translation_key'])) {
            $query->where('translation_key', $filter['translation_key']);
        }

        if (isset($filter['sort_field'], $filter['sort_order']) && !empty($filter['sort_field']) && !empty($filter['sort_order'])) {
            $query->order($filter['sort_field'], $filter['sort_order']);
        }
        return $query;
    }


}
